﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ModernShopping.AppCode.WebUtil
{
    public class ApproximatString
    {

        public static bool SoSanh(string Value, string Keyword)
        {
            int i, j, k, loi, saiSo;
            saiSo = (int)Math.Round(Value.Length * 0.3);
            if (Keyword.Length < (Value.Length - saiSo) || Keyword.Length > (Value.Length + saiSo))
                return false;
            i = j = loi = 0;
            while (i < Value.Length && j < Keyword.Length)
            {
                if (Value[i] != Keyword[j])
                {
                    loi++;
                    for (k = 1; k <= saiSo; k++)
                    {
                        if ((i + k < Value.Length) && Value[i + k] == Keyword[j])
                        {
                            i += k;
                            break;
                        }
                        else if ((j + k < Keyword.Length) && Value[i] == Keyword[j + k])
                        {
                            j += k;
                            break;
                        }
                    }
                }
                i++;
                j++;
            }
            loi += Value.Length - i + Keyword.Length - j;
            if (loi <= saiSo)
                return true;
            else return false;
        }
    }
}